﻿/********************************************************************************/
// <copyright file="UnityControllerFactory.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="17/12/2012 3:22:16 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Application
{

    using System;
    using System.Diagnostics;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Microsoft.Practices.Unity;
    [DebuggerStepThrough]
    public class UnityControllerFactory : DefaultControllerFactory
    {
        IUnityContainer _container;
        /// <summary>
        /// Initializes a new instance of the <see cref="UnityControllerFactory"/> class.
        /// </summary>
        /// <param name="unityContainer">The unity container.</param>
        public UnityControllerFactory(IUnityContainer unityContainer)
        {
            _container = unityContainer;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }

            if (!typeof(IController).IsAssignableFrom(controllerType))
            {
                throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controllerType.Name));
            }

            return _container.Resolve(controllerType) as IController;
        }
    }
}
